//
//  NormalVideoViewController.m
//  Dayou
//
//  Created by mac on 2018/8/14.
//  Copyright © 2018年 YangYang. All rights reserved.
//

#import "NormalVideoViewController.h"


#import <ZFPlayer/ZFPlayer.h>
#import <ZFPlayerControlView.h>
#import <ZFPlayer/ZFAVPlayerManager.h>
#import <ZFPlayer/ZFPlayerController.h>


static NSString *kVideoCover = @"https://upload-images.jianshu.io/upload_images/635942-14593722fe3f0695.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240";


@interface NormalVideoViewController ()
@property (nonatomic, strong) ZFPlayerController *playerController;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) UIButton *playBtn;

@property (nonatomic, strong) NSArray *videoArr;

@end

@implementation NormalVideoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}

-(void)createUI
{
    [self.view addSubview:self.containerView];
    [self.containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).mas_offset(0);
        make.top.mas_equalTo(self.view.mas_top).mas_offset(64 + 20);
        make.width.mas_equalTo(ScreenWidth - 20);
        make.height.mas_equalTo(200);
    }];
    
    [self.containerView addSubview:self.playBtn];
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.containerView.mas_centerX).mas_offset(0);
        make.centerY.mas_equalTo(self.containerView.mas_centerY).mas_offset(0);
    }];
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    self.playerController = [ZFPlayerController playerWithPlayerManager:playerManager containerView:self.containerView];
    self.playerController.controlView = self.controlView;
    self.playerController.assetURLs = self.videoArr;
    
    WeakSelf;
    self.playerController.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        NSLog(@"播放器在全屏与小屏之间切换");
    };
    #pragma mark - ----------播放结束之后，自动播放下一个视频----------
    self.playerController.playerDidToEnd = ^(id<ZFPlayerMediaPlayback>  _Nonnull asset) {
        StrongObj(weakSelf);
        [StrongweakSelf.playerController playTheNext];
        if (!StrongweakSelf.playerController.isLastAssetURL) {
            NSLog(@"开始播放下一个视频");
            NSString *title = [NSString stringWithFormat:@"视频标题【%zd】",StrongweakSelf.playerController.currentPlayIndex];
            [StrongweakSelf.controlView showTitle:title coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
        }
        else
        {
            NSLog(@"播放结束");
            [StrongweakSelf.playerController stop];
        }
    };
}

#pragma mark - -----------------播放按钮的响应事件-----------------
-(void)playClicked:(UIButton *)sender
{
    [self.playerController playTheIndex:0];
    [self.controlView showTitle:@"视频标题【0】" coverURLString:kVideoCover fullScreenMode:ZFFullScreenModeLandscape];
}

#pragma mark - ----------------------懒加载----------------------
-(UIView *)containerView
{
    if (!_containerView) {
        _containerView = [UIView new];
        _containerView.backgroundColor = [UIColor grayColor];
    }
    return _containerView;
}

-(ZFPlayerControlView *)controlView
{
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        
    }
    return _controlView;
}

-(UIButton *)playBtn
{
    if (!_playBtn) {
        _playBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_playBtn setImage:[UIImage imageNamed:@"play"] forState:UIControlStateNormal];
        [_playBtn addTarget:self action:@selector(playClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _playBtn;
}

-(NSArray *)videoArr
{
    if (!_videoArr) {
        _videoArr = @[
                      [NSURL URLWithString:@"https://www.apple.com/105/media/us/iphone-x/2017/01df5b43-28e4-4848-bf20-490c34a926a7/films/feature/iphone-x-feature-tpl-cc-us-20170912_1280x720h.mp4"],
                      [NSURL URLWithString:@"https://www.apple.com/105/media/cn/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/bruce/mac-bruce-tpl-cn-2018_1280x720h.mp4"],
                      [NSURL URLWithString:@"https://www.apple.com/105/media/us/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/peter/mac-peter-tpl-cc-us-2018_1280x720h.mp4"],
                      [NSURL URLWithString:@"https://www.apple.com/105/media/us/mac/family/2018/46c4b917_abfd_45a3_9b51_4e3054191797/films/grimes/mac-grimes-tpl-cc-us-2018_1280x720h.mp4"],
                      [NSURL URLWithString:@"http://flv3.bn.netease.com/tvmrepo/2018/6/H/9/EDJTRBEH9/SD/EDJTRBEH9-mobile.mp4"],
                      [NSURL URLWithString:@"http://flv3.bn.netease.com/tvmrepo/2018/6/9/R/EDJTRAD9R/SD/EDJTRAD9R-mobile.mp4"],
                      [NSURL URLWithString:@"http://dlhls.cdn.zhanqi.tv/zqlive/34338_PVMT5.m3u8"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-video/7_517c8948b166655ad5cfb563cc7fbd8e.mp4"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-smallvideo/68_20df3a646ab5357464cd819ea987763a.mp4"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-smallvideo/118_570ed13707b2ccee1057099185b115bf.mp4"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-smallvideo/15_ad895ac5fb21e5e7655556abee3775f8.mp4"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-smallvideo/12_cc75b3fb04b8a23546d62e3f56619e85.mp4"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-smallvideo/5_6d3243c354755b781f6cc80f60756ee5.mp4"],
                      [NSURL URLWithString:@"http://tb-video.bdstatic.com/tieba-movideo/11233547_ac127ce9e993877dce0eebceaa04d6c2_593d93a619b0.mp4"]];
    }
    return _videoArr;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.playerController stopCurrentPlayingCell];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
